<?php
namespace app\admin\controller;

use app\admin\model\Brand;
use app\admin\model\LengthUnit;
use app\admin\model\StockStatus;
use app\admin\model\WeightUnit;
use think\Request;
use app\admin\model\Sku;
use app\admin\model\Category;
use app\admin\model\Product as ProductModel;
use app\admin\validate\Product as ProductValidate;
use think\Image;
use think\Session;
use app\admin\model\Photo;
class Product extends Common {
    protected $product;
    protected $request;
    public function __construct(Request $request=null){
        parent::__construct($request);
        $this->request=Request::instance();
        $this->product=new  ProductModel();
    }
    public function index(){
        //获取搜索关键字
        $keyword=input('keyword',"");

        $where_title='';
        $where_upc='';
        if(!empty($keyword)){
            $where_title="title like '%$keyword%'";
            $where_upc="upc like '%$keyword'";
        }
        $pagesize=2;
        $data=$this->product->order("sort")
            ->where($where_title)
            ->whereOr($where_upc)
            ->field('id,title,upc,quantity,price,price_origin,category_id,create_time')
            ->paginate($pagesize,false,[
                'query'=>['keyword'=>$keyword
                ]
            ]);
        $this->assign('product_list',$data);
        return $this->fetch();
    }
    public function set(){
        if($this->request->isGet()){
            $id=input('id','');
            if(!empty($id)){
                $product=$this->product->where('id',$id)->find();
                $this->assign('product',$product);
            }else{
                $this->assign('product',null);
            }
            $sku = new Sku();
            $this->assign('sku_list', $sku->order('sort')->select());
            $stock = new StockStatus();
            $this->assign('stock_list', $stock->order('sort')->select());
            $length_unit = new LengthUnit();
            $this->assign('length_list', $length_unit->order('sort')->select());
            $weight_unit = new WeightUnit();
            $this->assign('weight_list', $weight_unit->order('sort')->select());
            $brand = new Brand();
            $this->assign('brand_list', $brand->order('sort')->select());
            $category = new Category();
            $this->assign('category_list', $category->order('sort')->select());
            return $this->fetch();
        }else{
            $data = $this->request->post();
            //存储当前登录用户的id
            $data['admin_id'] = Session::get('admin_id');
            //验证数据
            $validate = new ProductValidate();
            $is_pass = $validate->check($data);
            if (!$is_pass) {
                $this->error($validate->getError(), 'index');
            }
            if (!empty($id)) {
                $this->product=$this->product->where('id',$id)->find();
            }
            $res = $this->product->save($data);
            if ($res) {
                $this->success('设置产品成功', 'index');
            } else {
                $this->error('设置产品失败', 'index');
            }
        }
    }
    public function uploadImg()
    {
        $file = $this->request->file('file_data');
        //原图保存路径
        $path = ROOT_PATH . 'public' . DS . 'uploads' . DS . 'product';
        $info = $file->move($path);
        //缩略图名称
        $thumb_name = 'thumb_' . $info->getFilename();
        //缩略图保存路径和文件名称
        $thumb_path = ROOT_PATH . 'public' . DS . 'uploads' . DS . 'product' . DS . 'thumb' . DS . $thumb_name;
        if ($info) {
            //缩略图
            $image = Image::open($this->request->file('file_data'));
            $image->thumb(200, 200, Image::THUMB_CENTER)->save($thumb_path);
            return json([
                'status' => 1,
                'path' => $info->getSaveName(),
                'thumb' => 'thumb' . DS . $thumb_name
            ]);
        } else {
            return json([
                'status' => 0,
                'err' => $file->getError()
            ]);
        }
    }
    public function photo(){
        $id=input("id");
        $this->assign('id',$id);
        return $this->fetch();
    }
    public function uploadPhoto(){
        $file=$this->request->file('file_data');
        $path=ROOT_PATH.'public'.DS.'uploads'.DS.'product';
        $info=$file->move($path);
        if($info){
            $data=['img_url'=>$info->getSaveName(),
                'sort'=>1,
                'product_id'=>input('id')];
            $model = new Photo();
            $res=$model->save($data);
            return json(['status'=>1,'path'=>$info->getSaveName()]);
        }else{
            return json(['status'=>0,'err'=>$file->getError()]);
        }
    }
}